﻿@using Entidades.Sistema.Venta

@{
    Html.EnableClientValidation();
    Html.EnableUnobtrusiveJavaScript();

    var grid = @Html.DevExpress().GridView(
        settings => {
            settings.Name = "detailGridView_" + ViewData["IdPedido"];
            settings.KeyFieldName = "IdDetallePedido";
            settings.CallbackRouteValues = new { Controller = "Pedidos", Action = "EditPedidoDetallePartial", IdPedido = ViewData["IdPedido"] };
            settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Pedidos", Action = "EditPedidoDetalleAddNewPartial", IdPedido = ViewData["IdPedido"] };
            settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Pedidos", Action = "EditPedidoDetalleUpdatePartial", IdPedido = ViewData["IdPedido"] };
            settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Pedidos", Action = "EditPedidoDetalleDeletePartial", IdPedido = ViewData["IdPedido"] };
            settings.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
            settings.SettingsBehavior.ConfirmDelete = true;

            settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);
            
            settings.SettingsPopup.EditForm.Width = 600;
            settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.Center;
            settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;

            settings.CommandColumn.Visible = true;
            settings.CommandColumn.ShowNewButtonInHeader = true;
            settings.CommandColumn.ShowEditButton = true;
            
            settings.CommandColumn.ShowDeleteButton = true;

            settings.Columns.Add(
                column =>
                {
                    column.FieldName = "Producto.IdProducto";
                    column.Caption = "Producto";

                    column.ColumnType = MVCxGridViewColumnType.ComboBox;
                    var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
                    comboBoxProperties.DataSource = ViewBag.Producto;
                    comboBoxProperties.TextField = "Nombre";
                    comboBoxProperties.ValueField = "IdProducto";
                    comboBoxProperties.ValueType = typeof(int);
                    comboBoxProperties.TextFormatString = "{0}";
                    comboBoxProperties.EnableSynchronization = DefaultBoolean.False;
                    comboBoxProperties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;
                    comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;
                    comboBoxProperties.Columns.Add("Nombre", "Nombre", 180);
                    comboBoxProperties.Columns.Add("PrecioLista", "Precio", 60);
                    comboBoxProperties.Columns.Add("CantidadPorUnidadProd", "Cantidad", 60);
                    comboBoxProperties.Columns.Add("Unidad.Nombre", "Unidad", 80);
                });


            settings.Columns.Add("Cantidad");
            settings.Columns.Add("Producto.Unidad.Nombre").EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;


            settings.Columns.Add(column =>
            {
                column.FieldName = "Producto.PrecioLista";
                column.Caption = "P sin IVA";
                
                column.ColumnType = MVCxGridViewColumnType.SpinEdit;
                column.Width = 100;
                var spinEditProperties = column.PropertiesEdit as SpinEditProperties;
                spinEditProperties.DisplayFormatString = "c";
                spinEditProperties.DisplayFormatInEditMode = true;
                column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
            });

            settings.Columns.Add(column =>
            {
                column.FieldName = "Producto.Alicuota";
                column.Caption = "% IVA";

                column.ColumnType = MVCxGridViewColumnType.SpinEdit;
                column.Width = 50;
                column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
            });

            settings.Columns.Add(column =>
            {
                column.FieldName = "Producto.PrecioConIva";
                column.Caption = "P con IVA";

                column.ColumnType = MVCxGridViewColumnType.SpinEdit;
                column.Width = 100;
                var spinEditProperties = column.PropertiesEdit as SpinEditProperties;
                spinEditProperties.DisplayFormatString = "c";
                spinEditProperties.DisplayFormatInEditMode = true;
                column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
            });
            
            settings.Columns.Add(column =>
            {
                column.FieldName = "TotalRenglon";
                column.Caption = "TotalRenglon";

                column.ColumnType = MVCxGridViewColumnType.SpinEdit;
                column.Width = 100;
                var spinEditProperties = column.PropertiesEdit as SpinEditProperties;
                spinEditProperties.DisplayFormatString = "c";
                spinEditProperties.DisplayFormatInEditMode = true;
                column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
            });  
            
            settings.Columns.Add(
                column =>
                {
                    column.FieldName = "Estado";
                    column.Caption = "Estado";
                    column.ColumnType = MVCxGridViewColumnType.ComboBox;
                    var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
                    comboBoxProperties.DataSource = ViewBag.Estado;
                    comboBoxProperties.ValueType = typeof(string);
                    comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;
                    column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;  
                });           
            
            settings.Settings.ShowFooter = true;
            settings.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "TotalRenglon");
            settings.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "Cantidad");
            
         });

    if (ViewData["EditError"] != null){
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }    
}

@grid.Bind(Model).GetHtml()
    